# Create your views here.
from artwerx.models import Artwork, Tag
from django.views.generic import list_detail, simple

from django.utils import simplejson #For AJAXy awesomeness
from django.core.serializers import serialize #For even more AJAXy awesomeness
from django.http import HttpResponse #For simple raw HTTP responses

from random import randint #RANDOM! RANDOM!

#from django.http import Http404 #DELETE ME ASAP

def artwork_listByYear(request, year, paginate_by, queryset, allow_empty):
    thisyear = year
    return list_detail.object_list(
        request,
        extra_context = { 'year': thisyear, 'gallery_title': thisyear, 'gallery_type': 'year' },
        queryset = Artwork.objects.filter(year=year, is_visible_yearly=True),
        #paginate_by = 40,
        allow_empty = True,
    )

def artwork_listByYear_ajax(request, year):
    return simple.direct_to_template(
        request,
        template = 'mochikit_tables.html',
    )

def artwork_listByYear_json(request, year, paginate_by, queryset, allow_empty):
    thisyear = year
    queryset = Artwork.objects.filter(year=year, is_visible_yearly=True)
    fieldset = ('title', 'slug', 'medium', 'inch_height', 'inch_width', 'year', 'image_file', )
    json = serialize('json', queryset, fields=fieldset)
    return HttpResponse(json, mimetype='text/plain') #SHOULD BE 'application/json' !!!

def artwork_listByYear_xml(request, year, paginate_by, queryset, allow_empty):
    thisyear = year
    queryset = Artwork.objects.filter(year=year, is_visible_yearly=True)
    fieldset = ('title', 'slug', 'medium', 'inch_height', 'inch_width', 'year', 'image_file', )
    json = serialize('xml', queryset, fields=fieldset)
    return HttpResponse(json, mimetype='text/xml')

def artwork_single(request, slug):
    thisslug = slug
    return list_detail.object_detail(
        request,
        queryset=Artwork.objects.all(),
        slug = thisslug,
        slug_field = "slug"
#        extra_content = {'tag_list':Artwork.objects.filter(slug = thisslug).tags.filter(is_visible=True)}
#        extra_context = {'tag_list':Tag.objects.extra(where=['name IN (SELECT tag_id FROM artwerx_artwork_tags)']).filter(is_visible=True)}
#        extra_context = {'tag_list':Tag.objects.extra(where=['name IN (SELECT tag_id FROM artwerx_artwork_tags)']).filter(is_visible=True)}
    )    

def artwork_single_snip(request, slug):
    thisslug = slug
    return list_detail.object_detail(
        request,
        Artwork.objects.all(),
        slug = thisslug,
        slug_field = "slug",
        template_name = 'artwerx/ajax_single.html'
    )

def artwork_randomWithTagName(request, tag):
#IF there's any artwork with this tag, return a generic detail view of a random artwork
#ELSE return a generic simple template view
    taggedArtworkCount = Artwork.objects.filter(tags__name__exact=tag).count()
    if taggedArtworkCount == 0:
        return simple.direct_to_template(
            request,
            template = 'index.html',
        )
    randomIndex = randint(0, (taggedArtworkCount - 1))
    randomArtworkPK = Artwork.objects.filter(tags__name__exact=tag)[randomIndex]._get_pk_val()
    return list_detail.object_detail(
        request,
        queryset=Artwork.objects.filter(pk=randomArtworkPK),
        object_id=randomArtworkPK,
        template_name = 'index.html'
    )

def artwork_frontPage(request, tag):
#This view builds on artwork_randomWithTagName()
#IF there's any artwork with this tag, return a generic detail view of a random artwork
#ELSE return a generic simple template view
#ALSO pass a list of galleries to the template.
    taggedArtworkCount = Artwork.objects.filter(tags__name__exact=tag).count()
    if taggedArtworkCount == 0:
        #raise Http404
        return simple.direct_to_template(
		    request,
            template = 'index.html',
            #extra_context = {},
        )
    randomIndex = randint(0, (taggedArtworkCount - 1))
    randomArtworkPK = Artwork.objects.filter(tags__name__exact=tag)[randomIndex]._get_pk_val()
    return list_detail.object_detail(
        request,
        queryset=Artwork.objects.filter(pk=randomArtworkPK),
        object_id=randomArtworkPK,
        template_name = 'index.html',
        extra_context = {'tag_list':Tag.objects.extra(where=['name IN (SELECT tag_id FROM artwerx_artwork_tags)']).filter(is_visible=True)}
    )

def artwork_listByTag(request, tag):
    return list_detail.object_list(
        request,
        extra_context = { 'gallery_title': tag, 'gallery_type': 'tag' },
        queryset=Artwork.objects.filter(tags__name__exact=tag),
        #paginate_by = 40,
        allow_empty = True,
    )

def artwork_listByArtist(request, artist):
    return list_detail.object_list(
        request,
        extra_context = { 'gallery_title': artist, 'gallery_type': 'artist' },
        queryset=Artwork.objects.filter(artist=artist),
        #paginate_by = 40,
        allow_empty = True,
    )
